home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2415 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer Conversion
  5. Date: 20 Jan 1996 22:46 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <20JAN199622465412@erich.triumf.ca>
  9. References: <4ds4jq$fo4@su3.in.net>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4ds4jq$fo4@su3.in.net>, poundss@in.net (Sam Pounds) writes...
  14. >I am having a problem with a "string concatenate" function.
  15. >When I compile my little program it works, but I get a
  16. >"suspicious pointer" conversion warning. The function is
  17. >below and I call it with two strings that I want to concatenate.
  18. >char *my_strcat(const char *a, const char *b)
  19. >{
  20. >    char done[1024];
  21. >    char *p = done;
  22. >    while (*a)
  23. >      *p++ = *a++;
  24. >    while (*b)
  25. >      *p++ = *b++;
  26. >    *p = '\0';
  27. >    return done; /* this is the suspicious pointer conversion error */
  28. >}
  29.  
  30. Your function is declared to return a char pointer, and you are returning an
  31. array, which is not the same thing.  Sometimes char array names act like char
  32. pointers (particularly when used as function parameters), but not here.
  33.  
  34. A more important problem here is that "done" is an automatic array which will
  35. cease to exist when the function returns, possibly being over-written on the
  36. next call to another function.
  37.  
  38. You could declare "done" as a char pointer, and malloc() some memory for it to
  39. point to - this would solve both problems, but the calling function would have
  40. to free() that malloc()ed memory sometime.
  41.  
  42. You could also declare done as a static array (just add "static" before the
  43. present declaration), then the array will exist for the life of the program.
  44. I _think_ (and I know someone will correct me if I'm wrong) that changeing
  45. "return done;" to "return &done;" will fix the "suspicious pointer conversion"
  46. error.
  47.  
  48. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  49. Internet: bennett@triumf.ca         | of one another only when one can be
  50. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  51. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  52. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  53.  
  54.  
  55.  
  56.